package ldkxingzhe.ldkxingzhedemo.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;

import ldkxingzhe.ldkxingzhedemo.R;
import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;

/**
 * Created by ke on 16-3-21.
 */
public class SurfaceViewZOrderActivity extends Activity{
    @SuppressWarnings("unused")
    private static final String TAG = "SurfaceViewZOrderActivity";

    private FrameLayout firstFrame, secondFrame;
    private MSurfaceView surfaceView1, surfaceView2;
    private Button btnLeft, btnRight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_surface_z_order);
        firstFrame = (FrameLayout) findViewById(R.id.fl_first);
        secondFrame = (FrameLayout) findViewById(R.id.fl_second);
        btnLeft = (Button) findViewById(R.id.bt_left);
        btnRight = (Button) findViewById(R.id.bt_right);

        initSurfaceView();
        btnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v(TAG, "surface1below");
                surface1elow();
            }
        });

        btnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v(TAG, "surface2below");
                surface2Below();
            }
        });
    }

    private void surface1elow() {
        firstFrame.removeAllViews();
        secondFrame.removeAllViews();
        ViewGroup.LayoutParams params
                = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        surfaceView1.setZOrderMediaOverlay(true);
        surfaceView2.setZOrderMediaOverlay(false);
        firstFrame.addView(surfaceView2, params);
        secondFrame.addView(surfaceView1, params);
    }

    private void initSurfaceView() {
        surfaceView1 = new MSurfaceView(this, Color.RED);
        surfaceView2 = new MSurfaceView(this, Color.GREEN);
    }

    private void surface2Below() {
        firstFrame.removeAllViews();
        secondFrame.removeAllViews();
        ViewGroup.LayoutParams params =
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        surfaceView1.setZOrderMediaOverlay(false);
        surfaceView2.setZOrderMediaOverlay(true);
        firstFrame.addView(surfaceView1, params);
        secondFrame.addView(surfaceView2, params);
    }


    private class MSurfaceView extends SurfaceView implements SurfaceHolder.Callback{

        private final int backgroundColor;
        private SurfaceHolder holder;
        private Thread drawThread;

        public MSurfaceView(Context context, final int backgroundColor) {
            super(context);
            this.backgroundColor = backgroundColor;
            holder = getHolder();
            holder.addCallback(this);
            drawThread = new Thread(){
                @Override
                public void run() {
                    synchronized (holder){
                        LogDemo.v(TAG,"drawThread");
                        Canvas canvas = holder.lockCanvas();
                        canvas.drawColor(backgroundColor);
                        holder.unlockCanvasAndPost(canvas);
                    }
                }
            };
        }


        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            LogDemo.v(TAG, "surfaceCreated");
        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
            LogDemo.v(TAG, "surfaceChanged");
            drawThread.run();
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            LogDemo.v(TAG, "surfaceDestroyed");
        }
    }
}
